<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/8
 * Time: 14:47
 */

namespace App\Api\Admin\Controllers;

use  App\Models\MusicSearchLog;
use Illuminate\Http\Request;

class MusicController extends CommonController
{

    public function MusicSearchLog(Request $request){
     $page = max(1, (int) $request->input('page'));
     $startdate = empty(IT('startdate')) ? '' : IT('startdate'); //开始时间
     $enddate = empty(IT('enddate')) ? '': IT('enddate'); //结束时间
     $pagesize = (int) $request->input('pagesize');
     $pagesize = $pagesize ?: 10;
     $offset = ($page - 1) * $pagesize;
     if($enddate < $startdate){
            return json_error('结束时间要大于开始时间');
        }
     if($startdate == 'Invalid date'){
            $startdate = '';
      }
     if($enddate == 'Invalid date'){
            $enddate ='';
     }
     $log_list = MusicSearchLog::where(function($query) use($startdate,$enddate) {
          if(!empty($startdate) && !empty($enddate) ) {
             $query->whereBetween('created_at',[$startdate, $enddate]);
         }
     })->orderBy('created_at','desc')->take($pagesize)->skip($offset)->get();
     $total = MusicSearchLog::where(function($query) use($startdate,$enddate) {
         if(!empty($startdate) && !empty($enddate) ) {
             $query->whereBetween('created_at',[$startdate, $enddate]);}})->count();
     $totalPage = ceil($total / $pagesize);
     return json_success('ok',['total'=>$total,'totalPage' => $totalPage, 'list' => $log_list]);
    }
}